package com.office.personalOffice.workSchedule.controller;

import com.office.personalOffice.workSchedule.model.OaAgent;
import com.office.personalOffice.workSchedule.model.OaReader;
import com.office.personalOffice.workSchedule.model.OaSchedule;
import com.office.personalOffice.workSchedule.service.impl.WorkScheduleService;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/WorkSchedule")
public class WorkScheduleAction {

    @Autowired
    private WorkScheduleService workScheduleService;

    public WorkScheduleService getWorkScheduleService() {
        return workScheduleService;
    }

    public void setWorkScheduleService(WorkScheduleService workScheduleService) {
        this.workScheduleService = workScheduleService;
    }

    //启动页面渲染日程安排数据
    @ResponseBody
    @RequestMapping("/getScheduleList")
    public PageModel<OaSchedule> getScheduleList(@RequestBody Map<String,Object> mapModel){

        Integer pageNum = (Integer)mapModel.get("pageNum");
        String checkEmp = (String)mapModel.get("empName");
        String checkEmped = (String)mapModel.get("empedName");
        String checkEmpedDate = (String)mapModel.get("scheduleDate");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date parsedDate=null;
        if(checkEmpedDate!=null){
            try {
                parsedDate = sdf.parse(checkEmpedDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        PageModel<OaSchedule> oaSchedulePageModel = new PageModel<>();
        oaSchedulePageModel.setPageNum(pageNum);
        OaSchedule oaSchedule = new OaSchedule();
        oaSchedule.setLoginUserId(1);
        oaSchedule.setEmpName(checkEmp);
        oaSchedule.setEmpedName(checkEmped);
        oaSchedule.setScheduleDate(parsedDate);
        return workScheduleService.getScheduleListAfterPage(oaSchedulePageModel,oaSchedule);
    }

    //检查时间冲突
    @ResponseBody
    @RequestMapping("/CheckTime")
    public boolean CheckTime(@RequestBody Map<String,Object> mapModel){
        String scheduleDate = (String)mapModel.get("scheduleDate");
        String scheduleDeletetime = (String)mapModel.get("scheduleDeletetime");
        String scheduleEmpedId = (String)mapModel.get("scheduleEmpedId");
        Integer empedid = Integer.valueOf(scheduleEmpedId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date scheduleDateParsed=null;
        Date scheduleDeletetimeParsed=null;
        try {
            scheduleDateParsed = sdf.parse(scheduleDate);
            scheduleDeletetimeParsed = sdf.parse(scheduleDeletetime);
        } catch (ParseException e) {
            System.out.println(e);
        }
        OaSchedule oaSchedule = new OaSchedule();
        oaSchedule.setScheduleDate(scheduleDateParsed);
        oaSchedule.setScheduleDeletetime(scheduleDeletetimeParsed);
        oaSchedule.setScheduleEmpedId(empedid);
        return workScheduleService.CheckTime(oaSchedule);
    }

    //添加日程安排
    @ResponseBody
    @RequestMapping("/addSchedule")
    public boolean addSchedule(@RequestBody OaSchedule oaSchedule){
        return workScheduleService.addSchedule(oaSchedule);
    }

    //删除日程安排
    @ResponseBody
    @RequestMapping("/deleteSchedule")
    public boolean deleteSchedule(@RequestBody OaSchedule oaSchedule){
        return workScheduleService.deleteSchedule(oaSchedule)>0;
    }

    //去到修改日程安排，存入日程id到session
    @ResponseBody
    @RequestMapping("/toUpdateSchedule")
    public void toUpdateSchedule(@RequestBody OaSchedule oaSchedule,HttpSession session){
        long scheduleId = oaSchedule.getScheduleId();
        session.setAttribute("updatingScheduleId",scheduleId);
    }

    //去到修改日程安排，渲染页面
    @ResponseBody
    @RequestMapping("/updateScheduleInfo")
    public OaSchedule UpdateScheduleInfo(HttpSession session){
        long updatingScheduleId = (long)session.getAttribute("updatingScheduleId");
        OaSchedule oaSchedule = new OaSchedule();
        oaSchedule.setScheduleId(updatingScheduleId);
        List<OaSchedule> scheduleByCondition = workScheduleService.getScheduleByCondition(oaSchedule);
        return scheduleByCondition.get(0);
    }

    //修改日程安排
    @ResponseBody
    @RequestMapping("/updateSchedule")
    public boolean updateSchedule(@RequestBody OaSchedule oaSchedule,HttpSession session){
        long updatingScheduleId = (long)session.getAttribute("updatingScheduleId");
        oaSchedule.setScheduleId(updatingScheduleId);
        return workScheduleService.updateSchedule(oaSchedule)>0;
    }

    //渲染读者组信息
    @ResponseBody
    @RequestMapping("/getReaderList")
    public List<OaReader> getReaderList(HttpSession session){
        OaReader oaReader = new OaReader();
        oaReader.setReaderScheduleownerId(1);
        return workScheduleService.getReaderByCondition(oaReader);
    }

    //添加读者组信息
    @ResponseBody
    @RequestMapping("/addReader")
    public boolean addReader(@RequestBody OaReader oaReader){
        oaReader.setReaderScheduleownerId(1);
        oaReader.setReaderPower(1);
        return workScheduleService.addReader(oaReader)>0;
    }

    //更新读者授权状态
    @ResponseBody
    @RequestMapping("/deleteReader")
    public boolean deleteReader(@RequestBody Map<String,Object> mapTest){
        Map<String,String> readerInfo = (Map<String,String>)mapTest.get("readerInfo");
        Integer readerId = Integer.valueOf(readerInfo.get("readerId"));
        Integer readerEmpId = Integer.valueOf(readerInfo.get("readerEmpId"));
        OaReader oaReader = new OaReader();
        oaReader.setReaderEmpId(readerEmpId);
        oaReader.setReaderId(readerId);

        return workScheduleService.udpateReader(oaReader)>0;
    }

    //渲染代办组信息
    @ResponseBody
    @RequestMapping("/getAgentList")
    public List<OaAgent> getAgentList(HttpSession session){
        OaAgent oaAgent = new OaAgent();
        oaAgent.setAgentScheduleownerId(1);
        return workScheduleService.getAgentByCondition(oaAgent);
    }

    //查看可以代办的人员
    @ResponseBody
    @RequestMapping("/getAgentToOthers")
    public List<OaAgent> getAgentToOthers(HttpSession session){
        OaAgent oaAgent = new OaAgent();
        oaAgent.setAgentEmpId(1);
        return workScheduleService.getAgentedByCondition(oaAgent);
    }

    //添加代办组信息
    @ResponseBody
    @RequestMapping("/addAgent")
    public boolean addAgent(@RequestBody OaAgent oaAgent){
        oaAgent.setAgentScheduleownerId(1);
        oaAgent.setAgentUsable(1);
        return workScheduleService.addAgent(oaAgent)>0;
    }

    //更新代办者授权状态
    @ResponseBody
    @RequestMapping("/deleteAgent")
    public boolean deleteAgent(@RequestBody Map<String,Object> mapTest){
        Map<String,String> agentInfo = (Map<String,String>)mapTest.get("agentInfo");
        Integer agentId = Integer.valueOf(agentInfo.get("agentId"));
        Integer agentEmpId = Integer.valueOf(agentInfo.get("agentEmpId"));
        OaAgent oaAgent = new OaAgent();
        oaAgent.setAgentEmpId(agentEmpId);
        oaAgent.setAgentId(agentId);

        return workScheduleService.udpateAgent(oaAgent)>0;
    }



}
